<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="结算的对账批次号" prop="checkBatchNo">
        <el-input
          v-model="queryParams.checkBatchNo"
          placeholder="请输入结算的对账批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['property:clearing:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['property:clearing:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['property:clearing:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['property:clearing:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clearingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="结算的对账批次号" align="center" prop="checkBatchNo" />
      <el-table-column label="商户名称" align="center" prop="businessName" />
      <el-table-column label="结算金额" align="center" prop="amount" />
      <el-table-column label="交易日期" align="center" prop="tradeDateZf" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.tradeDateZf, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-button
      type="success"
      plain
      icon="el-icon-edit"
      size="mini"
      :disabled="single"
      @click="handleUpdateSure"
    >对账确认</el-button>
    <!-- 添加或修改结算管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户ID" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入商户ID" />
        </el-form-item>
        <el-form-item label="商户名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="对账年月日(yyyymmdd)" prop="checkDay">
          <el-input v-model="form.checkDay" placeholder="请输入对账年月日(yyyymmdd)" />
        </el-form-item>
        <el-form-item label="结算的对账批次ID" prop="checkBatchId">
          <el-input v-model="form.checkBatchId" placeholder="请输入结算的对账批次ID" />
        </el-form-item>
        <el-form-item label="结算的对账批次号" prop="checkBatchNo">
          <el-input v-model="form.checkBatchNo" placeholder="请输入结算的对账批次号" />
        </el-form-item>
        <el-form-item label="结算的对账批次明细ID" prop="checkBatchDetailId">
          <el-input v-model="form.checkBatchDetailId" placeholder="请输入结算的对账批次明细ID" />
        </el-form-item>
        <el-form-item label="交易流水号，支付订单的支付订单号" prop="payOrderNo">
          <el-input v-model="form.payOrderNo" placeholder="请输入交易流水号，支付订单的支付订单号" />
        </el-form-item>
        <el-form-item label="对账确认时间" prop="checkConfirmDate">
          <el-date-picker clearable
            v-model="form.checkConfirmDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择对账确认时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结算金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入结算金额" />
        </el-form-item>
        <el-form-item label="交易日期" prop="tradeDateZf">
          <el-date-picker clearable
            v-model="form.tradeDateZf"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择交易日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结算确认状态" prop="clearingConfirmState">
          <el-input v-model="form.clearingConfirmState" placeholder="请输入结算确认状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listClearing,
  getClearing,
  delClearing,
  addClearing,
  updateClearing,
  updateClearingEdit
} from "@/api/property/clearing";

export default {
  name: "Clearing",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算管理表格数据
      clearingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessId: null,
        businessName: null,
        checkDay: null,
        checkBatchId: null,
        checkBatchNo: null,
        checkBatchDetailId: null,
        payOrderNo: null,
        billType: null,
        checkConfirmDate: null,
        amount: null,
        tradeDateZf: null,
        clearingConfirmState: null,
        clearingConfirmUserId: null,
        clearingConfirmDate: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        checkDay: [
          { required: true, message: "对账年月日(yyyymmdd)不能为空", trigger: "blur" }
        ],
        checkBatchId: [
          { required: true, message: "结算的对账批次ID不能为空", trigger: "blur" }
        ],
        checkBatchNo: [
          { required: true, message: "结算的对账批次号不能为空", trigger: "blur" }
        ],
        checkBatchDetailId: [
          { required: true, message: "结算的对账批次明细ID不能为空", trigger: "blur" }
        ],
        checkConfirmDate: [
          { required: true, message: "对账确认时间不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "结算金额不能为空", trigger: "blur" }
        ],
        tradeDateZf: [
          { required: true, message: "交易日期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**对账确认*/
    handleUpdateSure(row){
      const id = row.id || this.ids
      console.log(id);
      updateClearingEdit(id).then(res=>{
        this.$message.success("确认成功");
        this.getList();
      })
    },
    /** 查询结算管理列表 */
    getList() {
      this.loading = true;
      listClearing(this.queryParams).then(response => {
        this.clearingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessId: null,
        businessName: null,
        checkDay: null,
        checkBatchId: null,
        checkBatchNo: null,
        checkBatchDetailId: null,
        payOrderNo: null,
        billType: null,
        checkConfirmDate: null,
        amount: null,
        tradeDateZf: null,
        clearingConfirmState: null,
        clearingConfirmUserId: null,
        clearingConfirmDate: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加结算管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClearing(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改结算管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClearing(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClearing(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除结算管理编号为"' + ids + '"的数据项？').then(function() {
        return delClearing(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('property/clearing/export', {
        ...this.queryParams
      }, `clearing_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
